package com.example.ui.other;

import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.ui.share.SeeActivity;
import com.example.util.Util;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

/**
 *问题反馈
 */
public class FeedbackActivity extends Activity implements OnClickListener{
	private Button leftBtn,rightBtn;
	private TextView titleText;
	private EditText feedbackContent;
	private RadioGroup feedType;
	private RadioButton consult,complaint,suggest;
	private String contentType;
	private String result,data;
	private String content;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other_feedback);
		init();
	}
	private void init() {
		leftBtn=(Button)findViewById(R.id.layout_title_left_btn);
		rightBtn=(Button)findViewById(R.id.layout_title_right_btn);
		titleText=(TextView)findViewById(R.id.layout_title_middle_text);
		leftBtn.setText("返回");
		rightBtn.setText("提交");
		titleText.setText("问题反馈");
		leftBtn.setOnClickListener(this);
		rightBtn.setOnClickListener(this);
		feedbackContent=(EditText)findViewById(R.id.other_feedback_content);
		feedType = (RadioGroup)findViewById(R.id.other_feedback_type);
		consult=(RadioButton)findViewById(R.id.other_feedback_consult);
		complaint=(RadioButton)findViewById(R.id.other_feedback_complaint);
		suggest=(RadioButton)findViewById(R.id.other_feedback_suggest);
		
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_title_left_btn:
			finish();
			break;
		case R.id.layout_title_right_btn:
			if(info()){
				new Thread(sumbit_run).start();
			}
			
			break;

		default:
			break;
		}
		
	}
	Runnable sumbit_run = new Runnable() {

		@Override
		public void run() {
			
			data = NetRequest.postRequestFeedback(contentType, content);
			result = AnalyzingJson.return_iaresult(data);
			if(result!=null){
			h.sendEmptyMessage(2);
			}
		}
	};
	private Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {

			case 2:
				if (result.equals("true")) {
					Toast.makeText(FeedbackActivity.this, "提交成功", Toast.LENGTH_LONG)
							.show();
				} else {
					Toast.makeText(FeedbackActivity.this, "提交失败，请重试",
							Toast.LENGTH_LONG).show();
				}

				break;
			default:
				break;
			}

		};

	};
	
	private boolean info() {
		 content = feedbackContent.getText().toString();
		if("".equals(content)){
			Util.toast(this, "您还没填写内容呢");
			return false;
		}else {
			if(consult.isChecked()){
				contentType="1";
				return true;
			}else if(complaint.isChecked()){
				contentType="2";
				return true;
			}else if(suggest.isChecked()){
				contentType="3";
				return true;
			}else{
				Util.toast(this, "请选择问题类型");
				return false;
			}
			
		}
		
	}
	

}
